﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Project
{
    public partial class advisorRegistration : System.Web.UI.Page
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["StudentAdvisorString"].ConnectionString;
        //SqlConnection connection = new SqlConnection(connectionString);
        //SqlCommand command_profile = null;
        //SqlCommand command_register = null;
        string[] days = new string[7];
        string[] time = new string[17];
        SqlConnection conn = new SqlConnection(connectionString);
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand comm = new SqlCommand();
        DataSet ds = new DataSet();
        SqlDataReader dr;
        string advisorid;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            conn.Open();
            comm = new SqlCommand("select advisorid from advisorinfo where advisorid='" + txt_advisorid.Text + "'", conn);
            dr = comm.ExecuteReader();
            while (dr.Read())
            {
                advisorid = dr[0].ToString();
            }
            conn.Close();




            if (txt_password.Text == txt_passwordagain.Text && txt_advisorid.Text != "" && txt_advisorlname.Text != "" && txt_advisorfname.Text != "" && txt_advisoremail1.Text != "" && txt_advisorphone.Text != "")
            {
                conn.Open();
                da = new SqlDataAdapter("insert into tblUserLogin values(" + txt_advisorid.Text + ",'" + txt_password.Text + "','" + txt_advisoremail1.Text + "','A',1,'','','')", conn);
                da.Fill(ds, "Login");
                conn.Close();

                TimeSpan time1 = TimeSpan.Parse(dd_time1.Text);
                TimeSpan time2 = TimeSpan.Parse(dd_time2.Text);
                if (dd_time1.SelectedIndex < dd_time2.SelectedIndex)
                {
                    for (int i = dd_time1.SelectedIndex; i < dd_time2.SelectedIndex; i++)
                    {
                        time[i] = time1.ToString();
                        time1 = time1 + TimeSpan.Parse("0:30");
                    }
                }
                else
                {
                    lbl_message.Text = "Invalid Time";
                    btn_reset_Click(sender, e);
                }
                if (chk_mon.Checked)
                {
                    days[0] = "True";
                    conn.Open();
                    da = new SqlDataAdapter("insert into Monday values('" + txt_advisorid.Text + "','" + txt_advisorfname.Text + " " + txt_advisorlname.Text + "','" + time[0] + "','" + time[1] + "','" + time[2] + "','" + time[3] + "','" + time[4] + "','" + time[5] + "','" + time[6] + "','" + time[7] + "','" + time[8] + "','" + time[9] + "','" + time[10] + "','" + time[11] + "','" + time[12] + "','" + time[13] + "','" + time[14] + "','" + time[15] + "','" + time[16] + "','" + dd_location.Text + "')", conn);
                    ds = new DataSet();
                    da.Fill(ds, "Monday");
                    conn.Close();
                }
                if (chk_tue.Checked)
                {
                    days[1] = "True";
                    conn.Open();
                    da = new SqlDataAdapter("insert into Tuesday values('" + txt_advisorid.Text + "','" + txt_advisorfname.Text + " " + txt_advisorlname.Text + "','" + time[0] + "','" + time[1] + "','" + time[2] + "','" + time[3] + "','" + time[4] + "','" + time[5] + "','" + time[6] + "','" + time[7] + "','" + time[8] + "','" + time[9] + "','" + time[10] + "','" + time[11] + "','" + time[12] + "','" + time[13] + "','" + time[14] + "','" + time[15] + "','" + time[16] + "','" + dd_location.Text + "')", conn);
                    ds = new DataSet();
                    da.Fill(ds, "Tuesday");
                    conn.Close();
                }
                if (chk_wed.Checked)
                {
                    days[2] = "True";
                    conn.Open();
                    da = new SqlDataAdapter("insert into Wednesday values('" + txt_advisorid.Text + "','" + txt_advisorfname.Text + " " + txt_advisorlname.Text + "','" + time[0] + "','" + time[1] + "','" + time[2] + "','" + time[3] + "','" + time[4] + "','" + time[5] + "','" + time[6] + "','" + time[7] + "','" + time[8] + "','" + time[9] + "','" + time[10] + "','" + time[11] + "','" + time[12] + "','" + time[13] + "','" + time[14] + "','" + time[15] + "','" + time[16] + "','" + dd_location.Text + "')", conn);
                    ds = new DataSet();
                    da.Fill(ds, "Wednesday");
                    conn.Close();
                }

                if (chk_thu.Checked)
                {
                    days[3] = "True";
                    conn.Open();
                    da = new SqlDataAdapter("insert into Thursday values('" + txt_advisorid.Text + "','" + txt_advisorfname.Text + " " + txt_advisorlname.Text + "','" + time[0] + "','" + time[1] + "','" + time[2] + "','" + time[3] + "','" + time[4] + "','" + time[5] + "','" + time[6] + "','" + time[7] + "','" + time[8] + "','" + time[9] + "','" + time[10] + "','" + time[11] + "','" + time[12] + "','" + time[13] + "','" + time[14] + "','" + time[15] + "','" + time[16] + "','" + dd_location.Text + "')", conn);
                    ds = new DataSet();
                    da.Fill(ds, "Thursday");
                    conn.Close();
                }
                if (chk_fri.Checked)
                {
                    days[4] = "True";
                    conn.Open();
                    da = new SqlDataAdapter("insert into Friday values('" + txt_advisorid.Text + "','" + txt_advisorfname.Text + " " + txt_advisorlname.Text + "','" + time[0] + "','" + time[1] + "','" + time[2] + "','" + time[3] + "','" + time[4] + "','" + time[5] + "','" + time[6] + "','" + time[7] + "','" + time[8] + "','" + time[9] + "','" + time[10] + "','" + time[11] + "','" + time[12] + "','" + time[13] + "','" + time[14] + "','" + time[15] + "','" + time[16] + "','" + dd_location.Text + "')", conn);
                    ds = new DataSet();
                    da.Fill(ds, "Friday");
                    conn.Close();
                }
                if (chk_sat.Checked)
                {
                    days[5] = "True";
                    conn.Open();
                    da = new SqlDataAdapter("insert into Saturday values('" + txt_advisorid.Text + "','" + txt_advisorfname.Text + " " + txt_advisorlname.Text + "','" + time[0] + "','" + time[1] + "','" + time[2] + "','" + time[3] + "','" + time[4] + "','" + time[5] + "','" + time[6] + "','" + time[7] + "','" + time[8] + "','" + time[9] + "','" + time[10] + "','" + time[11] + "','" + time[12] + "','" + time[13] + "','" + time[14] + "','" + time[15] + "','" + time[16] + "','" + dd_location.Text + "')", conn);
                    ds = new DataSet();
                    da.Fill(ds, "Saturday");
                    conn.Close();
                }
                if (chk_sun.Checked)
                {
                    days[6] = "True";
                    conn.Open();
                    da = new SqlDataAdapter("insert into Sunday values('" + txt_advisorid.Text + "','" + txt_advisorfname.Text + " " + txt_advisorlname.Text + "','" + time[0] + "','" + time[1] + "','" + time[2] + "','" + time[3] + "','" + time[4] + "','" + time[5] + "','" + time[6] + "','" + time[7] + "','" + time[8] + "','" + time[9] + "','" + time[10] + "','" + time[11] + "','" + time[12] + "','" + time[13] + "','" + time[14] + "','" + time[15] + "','" + time[16] + "','" + dd_location.Text + "')", conn);
                    ds = new DataSet();
                    da.Fill(ds, "Sunday");
                    conn.Close();
                }

                conn.Open();
                da = new SqlDataAdapter("insert into advisorinfo values(" + txt_advisorid.Text + ",'" + txt_advisorfname.Text + "','" + txt_advisorlname.Text + "', '" + txt_advisoremail1.Text + "','" + txt_advisoremail2.Text + "','" + txt_advisorphone.Text + "','" + dd_location.Text + "','" + days[0] + "','" + days[1] + "','" + days[2] + "','" + days[3] + "','" + days[4] + "','" + days[5] + "','" + days[6] + "','" + dd_time1.Text + "','" + dd_time2.Text + "','" + txt_password.Text + "','" + txt_advisorfname.Text + " " + txt_advisorlname.Text + "')", conn);
                ds = new DataSet();
                da.Fill(ds, "advisor");
                conn.Close();
                lbl_message.Text = "Registration Successful";
                Session["advisorlogin"] = txt_advisorid.Text;
                Response.Redirect("advisorprofile.aspx");
            }
            else
            {
                lbl_message.Text = "problem registering";
            }
        }

        protected void btn_reset_Click(object sender, EventArgs e)
        {
            txt_advisorid.Text = "";
            txt_advisorfname.Text = "";
            txt_advisorlname.Text = "";
            txt_advisoremail1.Text = "";
            txt_advisoremail2.Text = "";
            txt_advisorphone.Text = "";
            chk_fri.Checked = false;
            chk_thu.Checked = false;
            chk_mon.Checked = false;
            chk_tue.Checked = false;
            chk_wed.Checked = false;
            chk_sat.Checked = false;
            chk_sun.Checked = false;
            dd_time1.ClearSelection();
            dd_time2.ClearSelection();
            dd_location.ClearSelection();
            txt_password.Text = "";
            txt_passwordagain.Text = "";
            for (int i = 0; i < 7; i++)
            {
                days[i] = "False";
            }
        }

        //protected void btn_clear_Click(object sender, EventArgs e)
        //{
        //    txt_advisorID.Text = "";
        //    txt_firstName.Text = "";
        //    txt_lastName.Text = "";
        //    txt_password.Text = "";
        //    txt_confirmPassword.Text = "";
        //    txt_email.Text = "";
        //}

        //protected void btn_submit_Click(object sender, EventArgs e)
        //{
        //    string register_string = "insert into tblUserLogin with (XLOCK) (UserId,Password,Email,UserType) values (@advisorID,@password,@email,@userType)";
        //    command_register = new SqlCommand(register_string, connection);
        //    string profile_string = "insert into tblAdvisorInfo with (XLOCK) (AdvisorId,AdvisorFirstName,AdvisorLastName,EmailPrimary) values (@advisorID,@firstName,@lastName,@email)";
        //    command_profile = new SqlCommand(profile_string, connection);

        //    command_register.Parameters.AddWithValue("@advisorID", txt_advisorID.Text);
        //    command_register.Parameters.AddWithValue("@password", txt_password.Text);
        //    command_register.Parameters.AddWithValue("@email", txt_email.Text);
        //    command_register.Parameters.AddWithValue("@userType", "A");

        //    command_profile.Parameters.AddWithValue("@advisorID", txt_advisorID.Text);
        //    command_profile.Parameters.AddWithValue("@firstName", txt_firstName.Text);
        //    command_profile.Parameters.AddWithValue("@lastName", txt_lastName.Text);
        //    command_profile.Parameters.AddWithValue("@email", txt_email.Text);

        //    try
        //    {
        //        connection.Open();
        //        command_register.ExecuteNonQuery();
        //        command_profile.ExecuteNonQuery();
        //        connection.Close();
        //        lbl_thankyou.Text = "Thank you for your registration...";
        //    }
        //    catch (SqlException ex)
        //    {
        //        lbl_thankyou.Text = "This Advisor ID is already registered.";
        //    }
        //    finally
        //    {
        //        txt_advisorID.Text = "";
        //        txt_firstName.Text = "";
        //        txt_lastName.Text = "";
        //        txt_password.Text = "";
        //        txt_confirmPassword.Text = "";
        //        txt_email.Text = "";
        //    }
        //}




    }
}